package com.cskaoyan.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.L1VO;
import com.cskaoyan.vo.LevelCategoryVO;
import com.cskaoyan.wx.vo.GoodsCategoryVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<LevelCategoryVO> getLeveledList() {

        List<LevelCategoryVO> list = categoryMapper.getLeveledList();
        return list;
    }

    @Override
    public int update(Category category) {

        if ("".equals(category.getDesc().trim()))
            category.setDesc(null);
        if ("".equals(category.getIconUrl().trim()))
            category.setIconUrl(null);
        if ("".equals(category.getKeywords().trim()))
            category.setKeywords(null);
        if ("".equals(category.getLevel().trim()))
            category.setLevel(null);
        if ("".equals(category.getName().trim()))
            category.setName(null);
        if ("".equals(category.getPicUrl().trim()))
            category.setPicUrl(null);

        category.setUpdateTime(new Date());

        return categoryMapper.updateByPrimaryKeySelective(category);

    }

    @Override
    public int create(Category category) {

        category.setAddTime(new Date());
        return categoryMapper.insertSelective(category);
    }

    @Override
    public List<L1VO> getL1() {

        List<L1VO> list = categoryMapper.getL1();
        return list;
    }

    @Override
    public int delete(Category category) {
        Category c1 = new Category();
        c1.setId(category.getId());
        c1.setDeleted(true);
        c1.setUpdateTime(new Date());
        return categoryMapper.updateByPrimaryKeySelective(c1);
    }

    @Override
    public GoodsCategoryVO getGoodsCategory(int id) {

        Category category = categoryMapper.selectByPrimaryKey(id);

        int l1Id = id;
        if (category.getPid() != 0) l1Id = category.getPid();

        GoodsCategoryVO goodsCategory = categoryMapper.getGoodsCategory(l1Id);
        List<Category> brotherCategory = goodsCategory.getBrotherCategory();
        if (brotherCategory.size() > 0) {
            if (id == l1Id)
                goodsCategory.setCurrentCategory(brotherCategory.get(0));
            else
                goodsCategory.setCurrentCategory(brotherCategory.stream().filter(c -> c.getId() == id).findFirst().get());
        }

        return goodsCategory;
    }


    @Override
    public List<Goods> getGoods(String keyword, String sort, String order, Integer categoryId, Integer page, Integer size, String username,Integer brandId,Boolean isNew,Boolean isHot) {
        PageHelper.startPage(page, size);
        if (username != null && keyword != null){
            Integer userId = userMapper.selectUserIdByName(username);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setKeyword(keyword);
            searchHistory.setUserId(userId);
            // TODO: 这里暂时设置来源是微信端
            searchHistory.setFrom("wx");
            Date addTime = new Date();
            searchHistory.setAddTime(addTime);
            searchHistory.setUpdateTime(addTime);
            searchHistory.setDeleted(false);
            searchHistoryMapper.insert(searchHistory);
        }
        if (brandId == null && isHot == null && isNew == null) {
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            if (sort != null && order != null) {
                goodsExample.setOrderByClause(sort + " " + order);
            }
            if (keyword != null) {
                criteria.andKeywordsLike("%" + keyword + "%");
            }
            if (categoryId != null) {
                criteria.andCategoryIdEqualTo(categoryId);
            }
            criteria.andDeletedNotEqualTo(true);
            return goodsMapper.selectByExample(goodsExample);
        }
        else if (categoryId == null && isHot == null && isNew == null){
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andDeletedNotEqualTo(true);
            if (sort != null && order != null) {
                goodsExample.setOrderByClause(sort + " " + order);
            }
            if (brandId!=null) {
                criteria.andBrandIdEqualTo(brandId);
            }
            return goodsMapper.selectByExample(goodsExample);
        }
        else if (categoryId == 0 && brandId == null && isHot == null){
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andDeletedNotEqualTo(true);
            if (sort != null && order != null) {
                goodsExample.setOrderByClause(sort + " " + order);
            }
            if (isNew!=null) {
                criteria.andIsNewEqualTo(isNew);
            }
            return goodsMapper.selectByExample(goodsExample);
        }
        else if (categoryId == 0 && brandId == null && isNew == null){
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andDeletedNotEqualTo(true);
            if (sort != null && order != null) {
                goodsExample.setOrderByClause(sort + " " + order);
            }
            if (isHot!=null) {
                criteria.andIsHotEqualTo(isHot);
            }
            return goodsMapper.selectByExample(goodsExample);
        }
        return null;
    }
}
